<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    .box {
        margin: 50px auto;
        width: 300px;
        border: 1px solid #000000;
    }

    .box>.top {
        /*行内元素去除空隙*/
        font-size: 0;
    }

    .box>.top>button {
        height: 50px;
        font-size: 15;
        width: 60px;
        border: none;
        outline: none;
        text-align: center;


    }

    .bottom>div {
        display: none;
        height: 300px;
        background-color: yellow;
    }

    .box>.top>button.selected {
        background-color: green;
        color: red;
    }

    .bottom>div:nth-child(1) {
        display: block;
    }
</style>

<body>
    <div class="box">
        <div class="top">
            <button>按钮1</button>
            <button>按钮2</button>
            <button>按钮3</button>
            <button>按钮4</button>
            <button>按钮5</button>
        </div>
        <div class="bottom">
            <div>盒子1</div>
            <div>盒子2</div>
            <div>盒子3</div>
            <div>盒子4</div>
            <div>盒子5</div>
        </div>
    </div>
</body>
<script>
    window.onload = function () {
        var btns = document.getElementsByClassName('top')[0].children
        var divs = document.getElementsByClassName("bottom")[0].children
        //对button循环遍历 注册点击事件
        for (var i = 0; i < btns.length; i++) {
            //动态的给每一个button按钮添加一个属性  index并赋值1
            btns[i].index = i;
            btns[i].onclick = function () {

                //排他思想
                for (var j = 0; j < btns.length; j++) {

                    btns[j].className = '';
                    divs[j].style.display = 'none'
                }
                this.className = 'selected';
                divs[this.index].style.display = 'block'
            }
        }
    }

</script>

</html>